<template>
  <el-dialog
    v-model="show" width="1000px" :title="title" append-to-body :close-on-click-modal="false"
  >
    <el-form
      ref="matchRef" :model="form" :rules="rules" label-width="135px" :inline="true"
      :disabled="isView"
    >
      <el-row :gutter="20">
        <el-col>
          <el-form-item label="活动图片" prop="photos">
            <image-upload2 v-model="form.photos" :disabled="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" placeholder="请输入活动名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动时间" prop="hdTime" style="width: 100%">
            <el-date-picker
              v-model="form.hdTime"
              style="width:100%"
              clearable
              type="datetimerange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动地点" prop="address" style="width: 100%">
            <el-cascader
              v-model="regionIds"
              style="width:100%"
              :options="regionsList"
              :props="{ label:'text'}"
              placeholder="请选择省市区"
            />
            <el-input v-model="form.address" type="textarea" placeholder="请输入详细地址" rows="3" />
          </el-form-item>
        </el-col>
        <el-col :span="12" />
        <el-col :span="12">
          <el-form-item label="是否启用酒店预定" required :show-message="false">
            <el-radio-group v-model="form.isRoom">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isRoom==1" label="酒店可预订时间" prop="hqTime" style="width: 100%">
            <el-date-picker
              v-model="form.hqTime"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width:100%"
              clearable
              type="daterange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isRoom==1" label="酒店退订截止时间" prop="jdEnd" style="width: 100%">
            <el-date-picker
              v-model="form.jdEnd"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width: 100%"
              clearable
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="酒店退订截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isRoom==1" label="酒店下单截止时间" prop="jdLast" style="width: 100%">
            <el-date-picker
              v-model="form.jdLast"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width: 100%"
              clearable
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="酒店下单截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用车辆预定" required :show-message="false">
            <el-radio-group v-model="form.isCar">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isCar==1" label="车辆可预订时间" prop="carTime" style="width: 100%">
            <el-date-picker
              v-model="form.carTime"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width:100%"
              clearable
              type="daterange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isCar==1" label="车辆退订截止时间" prop="carOff" style="width: 100%">
            <el-date-picker
              v-model="form.carOff"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width: 100%"
              clearable
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="车辆退订截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isCar==1" label="车辆下单截止时间" prop="carLast" style="width: 100%">
            <el-date-picker
              v-model="form.carLast"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width: 100%"
              clearable
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="酒店下单截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用餐饮预定" required :show-message="false">
            <el-radio-group v-model="form.isFood">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isFood==1" label="餐饮可预订时间" prop="foodTime" style="width: 100%">
            <el-date-picker
              v-model="form.foodTime"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width:100%"
              clearable
              type="daterange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isFood==1" label="餐饮退订截止时间" prop="foodOff" style="width: 100%">
            <el-date-picker
              v-model="form.foodOff"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width: 100%"
              clearable
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="餐饮退订截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isFood==1" label="餐饮下单截止时间" prop="foodLast" style="width: 100%">
            <el-date-picker
              v-model="form.foodLast"
              :disabled="!form.hdTime"
              :disabled-date="disabledDate"
              style="width: 100%"
              clearable
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="餐饮下单截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="活动简介" prop="introduction" style="width: 100%;">
            <u-editor v-model="form.introduction" placeholder="请输入活动简介" :disabled="isView" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="预定说明" prop="reserveDes" style="width: 100%;">
            <u-editor v-model="form.reserveDes" placeholder="请输入预定说明" :disabled="isView" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone" style="width: 100%">
            <el-input v-model="form.phone" placeholder="联系电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票形式" prop="kpType" style="width: 100%">
            <el-checkbox-group v-model="kpTypeArr">
              <el-checkbox label="1">电子发票</el-checkbox>
              <el-checkbox label="2">纸质普票</el-checkbox>
              <el-checkbox label="3">纸质专票</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="发票开具说明" prop="remarks" style="width: 100%">
            <el-input v-model="form.remarks" type="textarea" rows="3" placeholder="发票开具说明" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!isView" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { getCurrentInstance, ref, toRefs } from 'vue'
import useVenStore from '@/store/modules/ven'
import { reactive } from '@vue/runtime-core'
import { addActivity, updateActivity } from '@/api/logex/activity'
import useUserStore from '@/store/modules/user'
import { computed } from '@vue/reactivity'
import { dayjs } from 'element-plus'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['submit'])

const show = ref(false)
const title = ref('')
const buttonLoading = ref(false)
const regionIds = ref([])
const kpTypeArr = ref([])
const isView = ref(false)

const data = reactive({
  form: {
    id: null,
    venueId: null,
    name: null,
    photos: null,
    province: null,
    city: null,
    area: null,
    address: null,
    introduction: null,
    reserveDes: null,
    phone: null,
    hdTime: null,
    hdStart: null,
    hdEnd: null,
    hqTime: null,
    hqStart: null,
    hqEnd: null,
    jdStart: null,
    jdEnd: null,
    jdLast: null,
    carTime: null,
    carStart: null,
    carEnd: null,
    carOff: null,
    carLast: null,
    foodTime: null,
    foodStart: null,
    foodEnd: null,
    foodOff: null,
    foodLast: null,
    kpType: null,
    status: '0',
    delStatus: '0',
    remarks: null,
    isRoom: 0,
    isCar: 0,
    isFood: 0
  },
  rules: {
    name: [
      { required: true, message: '活动名称不能为空', trigger: 'blur' }
    ],
    photos: [
      { required: true, message: '活动图片不能为空', trigger: 'blur' }
    ],
    hdTime: [
      { required: true, message: '活动时间不能为空', trigger: 'blur' }
    ],
    address: { validator: (rule, value, callback) => {
      if (!form.value.address || regionIds.value.length === 0) {
        callback(new Error('活动地点不能为空'))
      } else {
        callback()
      }
    }, required: true, trigger: 'blur' },
    hqTime: [
      { required: true, message: '酒店可预订时间不能为空', trigger: 'blur' }
    ],
    jdEnd: [
      { required: true, message: '酒店退订截止时间不能为空', trigger: 'blur' }
    ],
    jdLast: [
      { required: true, message: '酒店下单截止时间不能为空', trigger: 'blur' }
    ],
    carTime: [
      { required: true, message: '车辆可预订时间不能为空', trigger: 'blur' }
    ],
    carOff: [
      { required: true, message: '车辆退订截止时间不能为空', trigger: 'blur' }
    ],
    carLast: [
      { required: true, message: '车辆下单截止时间不能为空', trigger: 'blur' }
    ],
    foodTime: [
      { required: true, message: '餐饮可预订时间不能为空', trigger: 'blur' }
    ],
    foodOff: [
      { required: true, message: '餐饮退订截止时间不能为空', trigger: 'blur' }
    ],
    foodLast: [
      { required: true, message: '餐饮下单截止时间不能为空', trigger: 'blur' }
    ],
    introduction: [
      { required: true, message: '活动简介不能为空', trigger: 'blur' }
    ],
    reserveDes: [
      { required: true, message: '预定说明不能为空', trigger: 'blur' }
    ],
    phone: [
      { required: true, message: '联系电话不能为空', trigger: 'blur' }
    ],
    kpType: { validator: (rule, value, callback) => {
      if (kpTypeArr.value.length === 0) {
        callback(new Error('发票形式不能为空'))
      } else {
        callback()
      }
    }, required: true, trigger: 'change' },
    remarks: [
      { required: true, message: '发票开具说明不能为空', trigger: 'blur' }
    ]
  }
})
const { form, rules } = toRefs(data)

const regionsList = computed(() => useUserStore().regionsList)

function submitForm() {
  proxy.$refs['matchRef'].validate(valid => {
    if (valid) {
      form.value.province = regionIds.value[0]
      form.value.city = regionIds.value[1]
      form.value.area = regionIds.value[2]

      form.value.hdStart = form.value.hdTime[0]
      form.value.hdEnd = form.value.hdTime[1]
      form.value.hqStart = form.value.hqTime[0]
      form.value.hqEnd = form.value.hqTime[1]
      form.value.carStart = form.value.carTime[0]
      form.value.carEnd = form.value.carTime[1]
      form.value.foodStart = form.value.foodTime[0]
      form.value.foodEnd = form.value.foodTime[1]
      form.value.kpType = kpTypeArr.value.join(',')

      buttonLoading.value = true
      if (form.value.id != null) {
        updateActivity(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          show.value = false
          emit('submit')
        }).finally(() => {
          buttonLoading.value = false
        })
      } else {
        addActivity(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          show.value = false
          emit('submit')
        }).finally(() => {
          buttonLoading.value = false
        })
      }
    }
  })
}

function reset() {
  form.value = {
    id: null,
    venueId: null,
    name: null,
    photos: null,
    province: null,
    city: null,
    area: null,
    address: null,
    introduction: null,
    reserveDes: null,
    phone: null,
    hdTime: null,
    hdStart: null,
    hdEnd: null,
    hqTime: null,
    hqStart: null,
    hqEnd: null,
    jdStart: null,
    jdEnd: null,
    carTime: null,
    carStart: null,
    carEnd: null,
    carOff: null,
    foodTime: null,
    foodStart: null,
    foodEnd: null,
    foodOff: null,
    kpType: null,
    status: '0',
    delStatus: '0',
    remarks: null,
    isRoom: 0,
    isCar: 0,
    isFood: 0
  }
  proxy.resetForm('matchRef')
}

function disabledDate(date) {
  if (form.value.hdTime) {
    return date.getTime() > dayjs(form.value.hdTime[0]).valueOf()
  }
  return true
}

function checkDate(value) {
  const startTime = dayjs(value[0]).valueOf()
  if (form.value.hqTime && dayjs(form.value.hqTime[1]).valueOf() > startTime) {
    form.value.hqTime = null
  }
  if (form.value.jdEnd && dayjs(form.value.jdEnd).valueOf() > startTime) {
    form.value.jdEnd = null
  }
  if (form.value.jdLast && dayjs(form.value.jdLast).valueOf() > startTime) {
    form.value.jdLast = null
  }

  if (form.value.carTime && dayjs(form.value.carTime[1]).valueOf() > startTime) {
    form.value.carTime = null
  }
  if (form.value.carOff && dayjs(form.value.carOff).valueOf() > startTime) {
    form.value.carOff = null
  }
  if (form.value.carLast && dayjs(form.value.carLast).valueOf() > startTime) {
    form.value.carLast = null
  }

  if (form.value.foodTime && dayjs(form.value.foodTime[1]).valueOf() > startTime) {
    form.value.foodTime = null
  }
  if (form.value.foodOff && dayjs(form.value.foodOff).valueOf() > startTime) {
    form.value.foodOff = null
  }
  if (form.value.foodLast && dayjs(form.value.foodLast).valueOf() > startTime) {
    form.value.foodLast = null
  }
}

function open(params) {
  reset()

  show.value = true
  title.value = params.title
  isView.value = params.isView || false

  if (params.match) {
    form.value = params.match

    regionIds.value = []
    if (params.match.province) {
      regionIds.value.push(params.match.province)
    }
    if (params.match.city) {
      regionIds.value.push(params.match.city)
    }
    if (params.match.area) {
      regionIds.value.push(params.match.area)
    }

    form.value.hdTime = [form.value.hdStart, form.value.hdEnd]
    form.value.hqTime = [form.value.hqStart, form.value.hqEnd]
    form.value.carTime = [form.value.carStart, form.value.carEnd]
    form.value.foodTime = [form.value.foodStart, form.value.foodEnd]
    kpTypeArr.value = form.value.kpType.split(',')
  } else {
    form.value.venueId = useVenStore().venueInfo.id
    regionIds.value = []
  }
}
defineExpose({
  open
})
</script>

<style scoped>

</style>
